package com.board.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    //堂食订单的延迟队列
    @Bean
    public Queue seatOrderDeadQueue(){ //创建订单时间死信队列
        Map<String, Object> params = new HashMap<>();
        //定义该队列中消息的生存时间 单位毫秒 生存时间最终对应业务的延迟执行时间
        params.put("x-message-ttl", 60* 5 * 1000);
        // x-dead-letter-exchange 属性用于声明死信转发到的交换机
        params.put("x-dead-letter-exchange", "orderDirect");
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的路由键名称。
        params.put("x-dead-letter-routing-key", "seat_order");
        return new Queue("seat_order_dead_queue", true, false, false, params);
    }
    @Bean
    public Queue seatOrderQueue(){
        //处理订单15分钟后的普通队列
        return new Queue("seat_order_queue");
    }
    @Bean
    public DirectExchange orderTimeExchange(){
        //创建死信队列的交换机
        return new DirectExchange("orderDirect");
    }
    @Bean
    public Binding bindingOrderExchange(){ //绑定交换机和普通队列
        return BindingBuilder.bind(seatOrderQueue()).to(orderTimeExchange()).with("seat_order");
    }
    //外送订单的延迟队列
    @Bean
    public Queue sendOrderDeadQueue(){ //创建订单时间死信队列
        Map<String, Object> params = new HashMap<>();
        //定义该队列中消息的生存时间 单位毫秒 生存时间最终对应业务的延迟执行时间
        params.put("x-message-ttl", 60* 5 * 1000);
        // x-dead-letter-exchange 属性用于声明死信转发到的交换机
        params.put("x-dead-letter-exchange", "orderDirect");
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的路由键名称。
        params.put("x-dead-letter-routing-key", "send_order");
        return new Queue("send_order_dead_queue", true, false, false, params);
    }
    @Bean
    public Queue sendOrderQueue(){
        //处理订单15分钟后的普通队列
        return new Queue("send_order_queue");
    }
    @Bean
    public Binding bindingSendOrderExchange(){ //绑定交换机和普通队列
        return BindingBuilder.bind(sendOrderQueue()).to(orderTimeExchange()).with("send_order");
    }

}
